/*
 * WebspaceException.java
 *
 * Copyright(c) 2010
 * Michael Kroez
 * kroez@studiorutton.de
 *
 * All rights reserved.
 *
 * $Id$
 * 
 */

package de.studiorutton.access;

import java.text.MessageFormat;

import android.content.Context;
import de.studiorutton.gerengdic.LearningWordsApplication;
import de.studiorutton.gerengdic.R;

public class WebspaceException extends Exception {
	private static final long serialVersionUID = 5501957513239858841L;

	private String paramOne;
	private String paramTwo;
	private ErrorCode error;

	public enum ErrorCode {
		ERROR_UNKNOWN, ERROR_CANNOT_FIND_CONTENT_LENGTH, ERROR_CANNOT_FIND_CONTENT_TYPE, 
		ERROR_CANNOT_FIND_LAST_MODIFIED, ERROR_CANNOT_PARSE_LAST_MODIFIED, ERROR_CANNOT_REACH_REMOTE_SERVER,
		ERROR_CANNOT_REACH_REMOTE_FILE, ERROR_FILE_HAS_GZIP_ERROR
	};

	public WebspaceException(String string) {
		super(string);
	}

	public WebspaceException(Exception e) {
		super(e.getLocalizedMessage());
	}

	public WebspaceException(ErrorCode error) {
		this.error = error;
	}

	public WebspaceException(ErrorCode error, String param) {
		setErrorCodeAndParam(error, param);
	}

	public WebspaceException(ErrorCode error, String paramOne, String paramTwo) {
		setErrorCodeAndParams(error, paramOne, paramTwo);
	}

	public void setErrorCodeAndParam(ErrorCode error, String param) {
		this.paramOne = param;
		this.error = error;
	}

	public void setErrorCodeAndParams(ErrorCode error, String paramOne,
			String paramTwo) {
		this.error = error;
		this.paramOne = paramOne;
		this.paramTwo = paramTwo;
	}

	public String getLocalizedMessage() {
		Context c = LearningWordsApplication.get();

		int id = R.string.error_unknown;
		switch (error) {
		case ERROR_CANNOT_FIND_CONTENT_LENGTH:
			id = R.string.error_cannot_find_content_length;
			break;
		case ERROR_CANNOT_FIND_CONTENT_TYPE:
			id = R.string.error_cannot_find_content_type;
			break;
		case ERROR_CANNOT_FIND_LAST_MODIFIED:
			id = R.string.error_cannot_find_last_modified;
			break;
		case ERROR_CANNOT_PARSE_LAST_MODIFIED:
			id = R.string.error_cannot_parse_last_modified;
			break;
		case ERROR_CANNOT_REACH_REMOTE_SERVER:
			id = R.string.error_cannot_reach_remote_server;
			break;
		case ERROR_CANNOT_REACH_REMOTE_FILE:
			id = R.string.error_cannot_reach_remote_file;
			break;
		case ERROR_FILE_HAS_GZIP_ERROR:
			id = R.string.error_file_cannot_be_created;
			break;
		default:
			id = R.string.error_unknown;
		}

		String msg = c.getResources().getString(id);
		String result = null;
		if (paramTwo != null && paramOne != null) {
			result = MessageFormat.format(msg, paramOne, paramTwo );
		}
		else if (paramOne != null) {
			result = MessageFormat.format(msg, paramOne);
		} else
			result = msg;
		
		return result;
	}

}